package net.sky.oa.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class AsmsJdbcUtil {

	private static String URL ;
	private static String USERNAME ;
	private static String PASSWORD ;
	
	private static AsmsJdbcUtil instance = null;
	
	private AsmsJdbcUtil(){}
	
	synchronized public static AsmsJdbcUtil getInstance() {
		if(null == instance) {
			instance =  new AsmsJdbcUtil();
		}
		return instance;
	}
	
	static {
		try {
			InputStream in = AsmsJdbcUtil.class.getClassLoader().getResourceAsStream("conf/jdbc.properties");
			Properties p = new Properties();
			p.load(in);
			URL = p.getProperty("jdbc.asms.url");
			USERNAME = p.getProperty("jdbc.asms.username");
			PASSWORD = p.getProperty("jdbc.asms.password");
			Class.forName(p.getProperty("jdbc.asms.driverClassName"));
		} catch (Exception e) {			
			e.printStackTrace();
		}
	}
	
	public Connection getConnection() throws Exception {
		
		return DriverManager.getConnection(URL,USERNAME,PASSWORD);
	}
	
	public void release(ResultSet rs,Statement st,PreparedStatement pstmt, Connection conn) {
		try {
			if(null != rs) 
				rs.close();
		} catch (SQLException e) {
			//e.printStackTrace();
		} finally {
			try {
				if(null != st)
					st.close();
			} catch (SQLException e) {
				//e.printStackTrace();
			} finally {
				try {
					if(null != pstmt )
						pstmt.close();
				} catch (Exception e) {
					//e.printStackTrace();
				} finally {
					try {
						if(null != conn)
							conn.close();
					} catch (SQLException e) {
						//e.printStackTrace();
					}
				}
				
			}
		}
	}
	
}
